<jsp:include page="/templates/header.jsp"/>
<%@page import="database.Queries" %>
<%@page import="controller.*" %>
<%@page import="java.util.LinkedList" %>
<%@page import="java.sql.SQLException" %>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.sql.Connection" %>
<%@page import="database.Connect" %>
<%@page import="java.sql.SQLException"%>

<script type="text/javascript" src="/salonmorandsmigi/js/jquery.editable-1.3.3.js"></script>
<script type="text/javascript" src="/salonmorandsmigi/js/sinfo.js"></script>
	
<h1><%=request.getUserPrincipal().getName()%>'s Courses</h1>
	<%
	Connection con=null;
	try
	{
		con=Connect.getConnection();
		con.setAutoCommit(false);
		Student s = Queries.createStudent(con,request.getUserPrincipal().getName());
		con.commit();
	  	LinkedList<Course> courses = s.getCourses();
	   	if (courses==null || courses.size()==0){%>
	    	<div class="rediv">
	    		<h2>The student isn't registered to any course</h2>
	    	</div>
	    <%} 
	    else{%>
	    <div id="dialog" title="Are you sure?" style="display:none">
			<p>Are you sure you want to Unregister to that course?</p>
		</div>
	   	<div id="accordion" class="accordion">
	   	<% for (int i=0;i<courses.size();i++){ 
	   		Course current = courses.get(i);
	   		String name=current.getName();
	   		String username = request.getUserPrincipal().getName();
	   		name=URLEncoder.encode(name,"UTF-8");
	   	%>
	   		<h3><a href="#"><%=current.getName()%></a></h3>
		    <div>
		    	<p><b>Credit points:</b>&nbsp;&nbsp;<%=current.getPointNum()%></p>
		    	<p><b>Group number:</b>&nbsp;&nbsp;<%=current.getGroupNum() %></p>
		    		<div class="mycourses">
		    			<a class="user" name=<%=username%>></a>
		    			<button id="coursepage" name=<%=name%>>Course Page</button>
		    			<button id="unregister" name=<%=name%>>Unregister</button>
					</div>
		    
		    </div>
		   
	   	<%	}
	   		}
		}
		catch (SQLException e){
			con.rollback();
			  out.write("SERVER ERROR! PLEASE TRY AGAIN");
		}
		finally{
			if(con!=null) 
			{
				con.setAutoCommit(true);
			  	con.close();
			}
		}%>
		</div>
    
	
	
<jsp:include page="/templates/sidebar.jsp"/>
<jsp:include page="/templates/footer.jsp"/>